import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:totaldemo/getx/controller.dart';
import 'package:totaldemo/getx/dependency_lazyPut.dart';
import 'package:totaldemo/getx/dependency_put_find.dart';
import 'package:totaldemo/getx/getx_demo/getx_index.dart';
import 'package:totaldemo/getx/state_getx.dart';
import 'package:totaldemo/getx/state_getxbuilder.dart';
import 'package:totaldemo/getx/state_obx.dart';
import 'package:totaldemo/getx/state_workers.dart';

class GetXHomePage extends StatelessWidget {
  const GetXHomePage({Key key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    ///依赖注入，可以使用get.put获取实例
    final controller = Get.put(ObxController());
    return Scaffold(
      appBar: AppBar(
        title: Text('学习getx'),
      ),
      body: Column(
        mainAxisAlignment: MainAxisAlignment.start,
        children: [
          ElevatedButton(
              onPressed: () {
                Get.to(() => ObxPage());
              },
              child: Text('obx页面')),
          ElevatedButton(
              onPressed: () {
                Get.to(() => StateGetxPage());
              },
              child: Text('getx页面')),
          ElevatedButton(
              onPressed: () {
                Get.to(() => GetxBuilderPage());
              },
              child: Text('getxbuilder页面')),
          ElevatedButton(
              onPressed: () {
                Get.to(() => StateWorkersView());
              },
              child: Text('getx-防抖限流-workers')),
          ElevatedButton(
              onPressed: () {
                Get.to(() => DependencyPutFindView());
              },
              child: Text('依赖注入')),
          ElevatedButton(
              onPressed: () {
                Get.to(() => LazyPutPage());
              },
              child: Text('懒加载')),
          GestureDetector(
            onTap: () {
              Get.to(OtherPage(), arguments: {
                'name': '小王',
                'age': 15,
              });
            },
            child: Container(
              height: 200,
              width: 200,
              color: Colors.red,
              child: Align(
                alignment: Alignment.center,
                child: Obx(() {
                  return Text(
                    controller.count.toString(),
                    style: TextStyle(color: Colors.white, fontSize: 20),
                  );
                }),
              ),
            ),
          ),
          Text('控制器中de名字是：${controller.name}'),
          Row(children: controller.items.map((e) => Text('$e，')).toList())
        ],
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: () {
          controller.increment();
        },
        child: Icon(Icons.add),
      ),
    );
  }
}

class OtherPage extends StatelessWidget {
  ///共享其他页面的数据
  ///可以让Get找到一个正在被其他页面使用的controller，并将他返回给你
  final ObxController c = Get.find();
  String name;
  var getxMap = Get.arguments;
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        leading: GestureDetector(
            onTap: () {
              Get.back();
            },
            child: Icon(Icons.arrow_left_sharp)),
      ),
      body: Center(
        child: Column(
          children: [
            Text(
              // '${c.count}',
              '${Get.find<ObxController>().count}',
              style: TextStyle(fontSize: 40),
            ),
            Text(
              '从前面传过来的值是==>  ' + Get.arguments['name'],
              style: TextStyle(fontSize: 25),
            ),
            Divider(),
            ElevatedButton(
              onPressed: () {},
              child: Text('data'),
            ),
          ],
        ),
      ),
    );
  }
}
